package com.txby.platformApi.component.web;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.Constant;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformCore.entity.SysCusTopic;
import com.txby.platformCore.entity.SysOption;

/**
 * 医护人员账号管理，开设
 */
@RestController
@RequestMapping("/sys/exam")
public class ItemEntryController  extends CommonController {
	
	/**
	 * 所有试题列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:exam:list")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="SELECT t.*,t2.name deptName FROM  "+this.mBaseDao.tableName(SysCusTopic.class)+" t,sys_dept t2 WHERE t.deptId=t2.dept_id   " ;
		if(getUserId() != Constant.SUPER_ADMIN){
//			params.put("createUserId", getUserId());
			 join=getJoinSqlBracket();
			 sql+=" and t.deptId in "+getJoinSqlBracket();
		}
		String topic=params.get("topic")==null?"":params.get("topic").toString();
		if(!ChkUtil.isEmptyAllObject(topic)){
			if (topic.contains("%") || topic.contains("_")) {
				topic = topic.trim().replace("%", "\\%").replace("_", "\\_");
				topic = "%" + topic + "%";
				sql += " and t.topic like #{pojo.topic} escape '\\'";
				params.put("bedNO", "%" + topic.trim() + "%");
			} else {
				topic = "%" + topic.trim() + "%";
				sql += " and t.topic like #{pojo.topic} ";
				params.put("topic", "%" + topic.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by t.updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
 
	
	/**
	 * 试题信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:exam:info")
	public R info(@PathVariable("id") Long id){
		String sql="SELECT t.*,t2.name deptName FROM  "+this.mBaseDao.tableName(SysCusTopic.class)+" t,sys_dept t2 WHERE t.deptId=t2.dept_id and t.id_="+id+"" ;
		Map exam =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("exam", exam);
	}
	
	/**
	 * 保存试题
	 */
	@SysLog("保存试题")
	@RequestMapping("/save")
	@RequiresPermissions("sys:exam:save")
	public R save(@RequestBody SysCusTopic exam){
		try{
		ValidatorUtils.validateEntity(exam);
		String sql="select * from "+this.mBaseDao.tableName(SysCusTopic.class)+" where topic='"+exam.getTopic()+"'";
		SysCusTopic old=(SysCusTopic) this.mBaseDao.findBySqlUniqueObject(sql, SysCusTopic.class, null);
		 if(old!=null){
			 return R.ok().error("题干重复");
		 }
		 exam.setCreater(getUserId().toString());
		 exam.setUpdater(getUserId().toString());
		 this.mBaseDao.saveObject(exam);
		 if(exam.getType() == 3){//简答题默认给它一个空的文本框加入选项库
			 SysOption sysOption = new SysOption();
			 sysOption.setTopicId(new Integer(exam.getId_().toString()));
			 sysOption.setValue(null);
			 this.mBaseDao.saveObject(sysOption);
		 }
		 
		}catch (Exception e) {
				return R.error(5000,e.getMessage());
			}
		return R.ok();
	}
	
	/**
	 * 修改试题
	 */
	@SysLog("修改试题")
	@RequestMapping("/update")
	@RequiresPermissions("sys:exam:update")
	public R update(@RequestBody  SysCusTopic exam){
		try{
		ValidatorUtils.validateEntity(exam, AddGroup.class);
		   String sql="select * from "+this.mBaseDao.tableName(SysCusTopic.class)+" where topic='"+exam.getTopic()+"' and id_!="+exam.getId_()+" ";
		SysCusTopic old=(SysCusTopic) this.mBaseDao.findBySqlUniqueObject(sql, SysCusTopic.class, null);
		 if(old!=null){
			 return R.ok().error("题干重复");
		 }else{
			 old=(SysCusTopic) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(SysCusTopic.class)+" where   id_="+exam.getId_(), SysCusTopic.class, null);
			 this.mBaseDao.updateObject(exam);
			 if(exam.getType() == 3){//简答题默认给它一个空的文本框加入选项库
				 this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(SysOption.class)+" where topicId="+exam.getId_()+"", null);
				 SysOption sysOption = new SysOption();
				 sysOption.setTopicId(new Integer(exam.getId_().toString()));
				 sysOption.setValue(null);
				 this.mBaseDao.saveObject(sysOption);
			 }
		 }
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 删除试题
	 */
	@SysLog("删除试题")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:exam:delete")
	public R delete(@RequestBody Long[] ids){
		 for(Long id:ids){
		    	this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(SysCusTopic.class)+" where id_="+id+"", null);
		    	this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(SysOption.class)+" where topicId="+id+"", null);
		  }
			return R.ok();
	}
	/**
	 * 查询试卷
	 */
	@RequestMapping("/querypaperList")
	public R querysingleList(){
		String sql="select name text,id_ value from t_txby_bd_sysPaper";
		List querypaperList=this.mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("paperList", querypaperList);
	}
}
